
from patient_information import *

#由片段到患者
def cal_patient(all_id,all_label,all_pred):
    ture_label = list()  # 存放病人的真是标签
    pre_label = list()
    current_id = all_id[0]
    current_label = all_label[0]
    current_pred = np.zeros(2)
    for i in range(len(all_id)):
        if all_id[i] == current_id:
            current_pred += all_pred[i]
        else:
            current_pre_label = np.argmax(current_pred)
            pre_label.append(current_pre_label)
            ture_label.append(current_label)
            current_id = all_id[i]
            current_label = all_label[i]
            current_pred = all_pred[i]

    pre_label.append(current_pre_label)
    ture_label.append(current_label)
    return pre_label,ture_label

#多数投票
def cal_patient_voting(all_id,all_label,all_pred,Threshold = 0.5):
    ture_label = list()  # 存放病人的真是标签
    pre_label = list()
    current_id = all_id[0]
    current_label = all_label[0]
    current_pred = np.zeros(2)
    segment_label =list() #存放当前患者各个片段的预测标签
    for i in range(len(all_id)):
        if all_id[i] == current_id:
            current_pred += all_pred[i]
            segment_label.append( np.argmax(all_pred[i]))

        else:
            num_harsh = 0
            num_blowing = 0
            for label in segment_label : #统计片段结果
                if label == 0 :
                    num_harsh += 1
                else:
                    num_blowing += 1
            if  num_blowing / len(segment_label) < Threshold:
                current_pre_label = 0
            else  :
                current_pre_label = 1
            pre_label.append(current_pre_label)
            ture_label.append(current_label)
            segment_label = list()
            current_id = all_id[i]
            current_label = all_label[i]
            current_pred = all_pred[i]
            segment_label.append( np.argmax(all_pred[i]))

    pre_label.append(current_pre_label)
    ture_label.append(current_label)
    return pre_label,ture_label

def cal_patient_Max(all_id,all_label,all_pred):
    ture_label = list()  # 存放病人的真是标签
    pre_label = list()
    current_id = all_id[0]
    current_label = all_label[0]
    current_pred = np.zeros(2)
    segment_pre =list() #存放当前患者各个片段的预测标签
    for i in range(len(all_id)):
        if all_id[i] == current_id:
            current_pred += all_pred[i]
            segment_pre.append( all_pred[i])

        else:
            segment_pre = np.vstack(segment_pre)
            max_pre = np.max(segment_pre,axis=0)
            pre_harsh = max_pre[0]  #Harsh的最大概率
            pre_blowing = max_pre[1]
            if pre_harsh > pre_blowing :
                current_pre_label = 0
            elif pre_harsh < pre_blowing :
                current_pre_label = 1
            elif pre_harsh == pre_blowing : #数量一样，则按照概率平均
                current_pre_label = np.argmax(current_pred)
            pre_label.append(current_pre_label)
            ture_label.append(current_label)
            segment_pre = list()
            current_id = all_id[i]
            current_label = all_label[i]
            current_pred = all_pred[i]
            segment_pre.append( all_pred[i])

    pre_label.append(current_pre_label)
    ture_label.append(current_label)
    return pre_label,ture_label